﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using UCL.Powersource.ViewModels;
using UCL.Powersource.ViewModels.Analysis;
using UCL.Powersource.Views.Dialogs;

namespace UCL.Powersource.Views
{
    /// <summary>
    /// Interaction logic for ReportingView.xaml
    /// </summary>
    public partial class ReportingView : UserControl
    {

        public ReportingViewModel VM { get { return (ReportingViewModel)(this.DataContext); } }

        public ReportingView()
        {
            InitializeComponent();
            this.Loaded += ReportingView_Loaded;
            this.Unloaded += ReportingView_Unloaded;

        }

        void ReportingView_Unloaded(object sender, RoutedEventArgs e)
        {
            VM.Deactivate();
        }

        void ReportingView_Loaded(object sender, RoutedEventArgs e)
        {
            //var pe = lbSelectableTfsTeamProjectConfigurations;
            if (VM != null)
            {
                VM.FireCannotDeleteError -= VM_FireCannotDeleteError;
                VM.FireCannotDeleteError += VM_FireCannotDeleteError;

                VM.FireDeleteConfirmationDialog -= VM_FireDeleteConfirmationDialog;
                VM.FireDeleteConfirmationDialog += VM_FireDeleteConfirmationDialog;

                VM.FireRecalculateDialog -= VM_FireRecalculateDialog;
                VM.FireRecalculateDialog += VM_FireRecalculateDialog;

                VM.PropertyChanged -= VM_PropertyChanged;
                VM.PropertyChanged += VM_PropertyChanged;
                VM.Activate();
            }
        }

        void VM_FireRecalculateDialog(object sender, EventArgs e)
        {
            var result = ShowConfirmationDialog(VM.RecalculateMsg, VM.RecalculateCaption);

            switch (result)
            {
                case MessageBoxResult.OK:
                    VM.ForceRecalculate();
                    break;
                case MessageBoxResult.Cancel:
                    break;
            }
        }

        void VM_FireDeleteConfirmationDialog(object sender, EventArgs e)
        {
            var result = ShowConfirmationDialog(VM.DeleteConfirmationMsg, VM.DeleteConfirmationCaption);
            
            switch (result)
            {
                case MessageBoxResult.OK:
                    VM.DeleteCurrentReportingContext();
                    break;
                case MessageBoxResult.Cancel:
                    break;
            }
        }

        private MessageBoxResult ShowConfirmationDialog(string message, string caption)
        {
            var button = MessageBoxButton.OKCancel;
            var icon = MessageBoxImage.Question;

            return System.Windows.MessageBox.Show(message, caption, button, icon);
        }

        void VM_FireCannotDeleteError(object sender, EventArgs e)
        {
            var button = MessageBoxButton.OK;
            var icon = MessageBoxImage.Exclamation;

            var result = System.Windows.MessageBox.Show(VM.CannotDeleteMsg, VM.CannotDeleteCaption, button, icon);
        }

        void VM_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            bool useVSTransition = true;
            if (Helpers.PropertyExtensions.IsPropertyChanged(e, () => VM.CurrentStatisticViewModel))
            {
                if (VM.CurrentStatisticControl == null)
                {
                    var vmBorder = new Border() { HorizontalAlignment = System.Windows.HorizontalAlignment.Center, VerticalAlignment = System.Windows.VerticalAlignment.Center};
                    vmBorder.Child = new TextBlock() { Text = "No statistic control to show", FontSize = 16 , FontStyle=FontStyles.Italic  ,Foreground=Brushes.DarkGray};
                    bdStatistics.Child = vmBorder;
                }
                else
                {
                    bdStatistics.Child = VM.CurrentStatisticControl;
                    //bdStatistics.InvalidateVisual();
                    VM.CurrentStatisticControl.InvalidateMeasure();
                    VM.CurrentStatisticControl.InvalidateVisual();
                }
            }

            if (Helpers.PropertyExtensions.IsPropertyChanged(e, () => VM.IsCalculating))
            {
                if (VM.IsCalculating)
                {
                    VisualStateManager.GoToElementState(this, "IsCalculating", useVSTransition);
                }
                else
                {
                    VisualStateManager.GoToElementState(this, "NotIsCalculating", useVSTransition);
                }
            }

            if (Helpers.PropertyExtensions.IsPropertyChanged(e, () => VM.CurrentStatisticViewModel))
            {
                
                if (VM.CurrentStatisticControl == null)
                {

                }
                else
                {
                    

                    if (VM.CurrentStatisticViewModel.IsConsumingActiveStatistic)
                    {
                        VisualStateManager.GoToElementState(this, "IsConsumingActiveStatistic", useVSTransition);
                    }
                    else
                    {
                        VisualStateManager.GoToElementState(this, "NotIsConsumingActiveStatistic", useVSTransition);
                    }

                    if (VM.CurrentStatisticViewModel.IsConsumingShowNormalized)
                    {
                        VisualStateManager.GoToElementState(this, "IsConsumingNormalizedValues", useVSTransition);
                    }
                    else
                    {
                        VisualStateManager.GoToElementState(this, "NotIsConsumingNormalizedValues", useVSTransition);
                    }
                    switch (VM.CurrentStatisticViewModel.StatisticViewModelType)
                    {
                        case StatisticViewModelTypes.SingleTeam_SingleTimeFrame:
                        case StatisticViewModelTypes.SingleTeam_MultipleTimeFrames:
                            VisualStateManager.GoToElementState(this, "SingleTeam", useVSTransition);
                            break;

                        case StatisticViewModelTypes.MultipleTeams_SingleTimeFrame:
                        case StatisticViewModelTypes.MultipleTeams_MultipleTimeFrames:
                            VisualStateManager.GoToElementState(this, "MultipleTeams", useVSTransition);
                            break;
                        default:
                            break;
                    }
                    switch (VM.CurrentStatisticViewModel.StatisticViewModelType)
                    {
                        case StatisticViewModelTypes.SingleTeam_SingleTimeFrame:
                        case StatisticViewModelTypes.MultipleTeams_SingleTimeFrame:
                            VisualStateManager.GoToElementState(this, "SingleTimeFrame", useVSTransition);
                            break;
                        case StatisticViewModelTypes.MultipleTeams_MultipleTimeFrames:
                        case StatisticViewModelTypes.SingleTeam_MultipleTimeFrames:
                            VisualStateManager.GoToElementState(this, "MultipleTimeFrames", useVSTransition);
                            break;
                        default:
                            break;
                    }
                }
            }
        }

        private void RequestNewContextName(object sender, RoutedEventArgs e)
        {
            if (VM != null)
            {
                var dialog = new ReportingContextDialog(VM);
                dialog.ShowDialog();
            }
        }

        private void Expander_Expanded_1(object sender, RoutedEventArgs e)
        {

        }

        int i = 0;

        private void Debug_Click_1(object sender, RoutedEventArgs e)
        {
            //var r = new String[] { "SingleTeam", "SingleTimeFrame", "MultipleTeams", "MultipleTimeFrames", "IsConsumingActiveStatistic", "NotIsConsumingActiveStatistic" };
            //if (i >= r.Length) i = 0;
            ////Start debugging here
            //var gr=VisualStateManager.GetVisualStateGroups(this);
            

            //VisualStateManager.GoToElementState(this, r[i], false);
            i++;
        }

        private void lbSelectableTfsTeamProjectConfigurations_Selected_1(object sender, RoutedEventArgs e)
        {
            var r = new List<String>();
            foreach (var si in lbSelectableTfsTeamProjectConfigurations.SelectedItems)
            {
                r.Add(si.ToString());
            }
            var conn = PowersourceConnector.GetExtensibilityPoint();
            conn.Log.Debug("Teams selected: {0}", string.Join(", ", r));
        }

        private void lbSelectableTfsTeamProjectConfigurations_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            var r = new List<String>();
            foreach (var si in lbSelectableTfsTeamProjectConfigurations.SelectedItems)
            {
                r.Add(si.ToString());
            }
            var conn = PowersourceConnector.GetExtensibilityPoint();
            conn.Log.Debug("Teams selected: {0}", string.Join(", ", r));

        }
    }
}
